const {ccclass, property} = cc._decorator;
declare let eManager, dbh, DBManager, i18n, JsonHttp;
import BaseView from "../../component/BaseView";
import HzMiniSelectIconList from "./HzMiniSelectIconList";
import Utils = require("../../utils/Utils");
import PopUIHelper = require("../../utils/PopUIHelper");
import UIHelper = require("../../component/UIHelper");

@ccclass
export default class RepairEyesView extends BaseView {
    @property(HzMiniSelectIconList)
    selectList: HzMiniSelectIconList = null;

    @property(cc.Node)
    guideNodeList: cc.Node[] = [];

    @property(sp.Skeleton)
    startSpine: sp.Skeleton = null;

    @property(sp.Skeleton)
    eyesSpine: sp.Skeleton = null;

    @property(sp.Skeleton)
    endSpine: sp.Skeleton = null;

    @property(cc.Node)
    endNode: cc.Node = null;

    @property(cc.Node)
    btnClose: cc.Node = null;

    _point = 1;
    _isPlay = false;
    _Step = ["mianqian", "niezi", "mianqian", "niezi"];//关卡步骤图标拼接
    _StepNum = 4;
    _isStart = false;//是否是剧情前触发
    onLoad() {
        super.onLoad();
        Utils.audioManager.stopBGM();
        //@ts-ignore
        this._isStart = this.node.openParam;
    };

    onClose() {
        Utils.audioManager.playDefaultBGM();
    };

    start() {
        super.start();
        this.btnClose.active = !this._isStart;
        this.eyesSpine.setCompleteListener(() => {
            this.showGuide();
        });
        this.startSpine.setCompleteListener(() => {
            this.startSpine.node.parent.active = false;
            this.eyesSpine.node.parent.active = true;
            this.eyesSpine.setAnimation(0, "00", false);
            Utils.audioManager.playSound("huazhuang/eyes/step2_00");
        });
        this.startSpine.setAnimation(0, "animation", false);
        Utils.audioManager.playSound("huazhuang/eyes/step1");
        this.selectList.showList(this._Step, this._StepNum, 1);
    };

    showGuide() {
        let guide = this.guideNodeList[this._point - 1];
        if (guide) {
            guide.active = true;
        }
    };

    onClickPlay(event, index) {
        let _index = parseInt(index);
        if (this._isPlay)
            return;
        let guide = this.guideNodeList[_index - 1];
        if (guide) {
            guide.active = false;
        }
        this.eyesSpine.setCompleteListener(() => {
            this._point++;
            this.selectList.showList(this._Step, this._StepNum, this._point);
            this._isPlay = false;
            this.showGuide();
            this.showPlayEnd();
        });
        this.eyesSpine.setAnimation(0, "0" + index, false);
        Utils.audioManager.playSound("huazhuang/eyes/step2_0" + index);
        this._isPlay = true;
    };

    showPlayEnd() {
        if (this._point <= 4)
            return;
        this.eyesSpine.node.parent.active = false;
        this.endSpine.node.parent.active = true;
        this.endSpine.setCompleteListener(() => {
            this.endNode.active = true;
            Utils.audioManager.playSound("huazhuang/mini/mini_jiesuan");
        });
        this.endSpine.setAnimation(0, "animation", false);
        Utils.audioManager.playSound("huazhuang/eyes/step3");
    };

    onClickEnd(isSkip) {
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    onClickSkip() {
        this.onClickEnd(true);
    };
}
